home *** CD-ROM | disk | FTP | other *** search
- /*
- * index.c
- * ++jrb bammi@dsrgsun.ces.CWRU.edu
- *
- * Usage: index [infile] [outfile]
- *
- */
-
- #include <stdio.h>
-
- #ifdef DLIBS
- #include <string.h>
- #endif
-
- typedef struct _pagelist {
- int page;
- struct _pagelist *next;
- } PAGELIST;
-
- typedef struct {
- char *name;
- PAGELIST *pagelist;
- } ITEM;
-
- ITEM **itemlist;
- int nitem = 0;
- int maxitem = 0;
-
- FILE *in, *out, *fopen();
- char *rindex(), *malloc(), *realloc(), *strcpy();
- char infile[128], outfile[128];
-
- #ifdef ATARI
- long _stksize = 8L * 1024L;
- #endif
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- switch(argc)
- {
- case 1:
- strcpy(infile, "STDIN");
- strcpy(outfile, "STDOUT");
- in = stdin;
- out = stdout;
- break;
-
- case 2:
- if((in = fopen(*++argv, "r")) == (FILE *)NULL)
- {
- perror(*argv);
- exit(1);
- }
- strcpy(infile, *argv);
- basename(outfile, *argv);
- strcat(outfile,".ind");
- if((out = fopen(outfile, "w")) == (FILE *)NULL)
- {
- perror(outfile);
- exit(2);
- }
- break;
-
- case 3:
- if((in = fopen(*++argv, "r")) == (FILE *)NULL)
- {
- perror(*argv);
- exit(3);
- }
- strcpy(infile, *argv);
- if((out = fopen(*++argv, "w")) == (FILE *)NULL)
- {
- perror(*argv);
- exit(4);
- }
- strcpy(outfile, *argv);
- break;
-
- default:
- fprintf(stderr,"Usage: %s [infile] [outfile]\n",
- #ifdef ATARI
- "index");
- #else
- argv[0]);
- #endif
- exit(5);
- }
-
- readin();
- fclose(in);
-
- printout();
- fclose(out);
-
- exit(0);
- }
-
- #ifdef ATARI
- #define SEP '\\'
- #else
- #define SEP '/'
- #endif
-
- basename(s, name)
- register char *s, *name;
- {
- register char *p, *q;
- extern char *rindex();
-
- if((p = rindex(name, SEP)) != (char *)NULL)
- p = rindex(p, '.');
- else
- p = rindex(name, '.');
-
- if(p == (char *)NULL)
- {
- strcpy(s, name);
- return;
- }
-
- for(q = name; q != p; q++, s++)
- *s = *q;
-
- *s = '\0';
- }
-
- readin()
- {
- char line[1024];
- extern char *fgets();
-
- while(fgets(line, 1024, in) != (char *)NULL)
- process(line);
- }
-
- process(l)
- register char *l;
- {
- char name[1024];
- char page[16];
- register char *p, *q;
- register int level;
- extern int atoi();
-
- for(p = l; *p != '{'; p++)
- {
- if(*p == '\0')
- {
- fprintf(stderr,"Skipped Illegal Entry %s\n", l);
- return;
- }
- }
-
- for (q = name, level = 0, p++; !((*p == '}') && (level == 0));
- p++, q++)
- {
- *q = *p;
- if(*p == '{')
- level++;
- else if (*p == '}')
- level--;
- else if (*p == '\0')
- {
- fprintf(stderr,"Skipped Illegal Entry %s\n", l);
- return;
- }
- }
-
- *q = '\0';
-
- for(p++; *p != '{'; p++)
- {
- if(*p == '\0')
- {
- fprintf(stderr,"Skipped Illegal Entry %s\n", l);
- return;
- }
- }
- for (q = page, level = 0, p++; !((*p == '}') && (level == 0));
- p++, q++)
- {
- *q = *p;
- if(*p == '{')
- level++;
- else if (*p == '}')
- level--;
- else if (*p == '\0')
- {
- fprintf(stderr,"Skipped Illegal Entry %s\n", l);
- return;
- }
- }
-
- *q = '\0';
-
- enter(name, atoi(page));
- }
-
- enter(nam, num)
- char *nam;
- int num;
- {
- register int i, r;
-
- for(i = 0; i < nitem; i++)
- {
- if((r = strcmp(itemlist[i]->name, nam)) == 0)
- {
- addpage(i, num);
- return;
- }
- else
- {
- if(r > 0)
- {
- additem( i, nam, num);
- return;
- }
- }
- }
- additem(nitem, nam, num);
- }
-
- additem(pos, nam, num)
- int pos;
- char *nam;
- int num;
- {
- register ITEM *item;
- register int i;
- extern char *myalloc();
- extern char *strcpy();
- extern int strlen();
-
- if(nitem >= maxitem)
- expandit();
- if(pos < nitem)
- {
- for(i = nitem; i > pos; i--)
- itemlist[i] = itemlist[i-1];
- }
-
- item = (ITEM *)myalloc(sizeof(ITEM));
- item->name = strcpy(myalloc(strlen(nam) + 1), nam);
- item->pagelist = (PAGELIST *)NULL;
- itemlist[pos] = item;
- addpage(pos, num);
- nitem++;
-
- }
-
- #define CHUNKSIZE 10
-
- expandit()
- {
- extern char *myalloc();
- extern char *realloc();
-
-
- if(maxitem == 0)
- {
- itemlist = (ITEM **)myalloc(sizeof(ITEM *) * CHUNKSIZE);
- maxitem = CHUNKSIZE;
- return;
- }
-
- if((itemlist = (ITEM **)realloc(itemlist, sizeof(ITEM *) * (maxitem +
- CHUNKSIZE))) == (ITEM **)NULL)
- {
- fprintf(stderr,"Not enough memory\n");
- exit(7);
- }
- maxitem += CHUNKSIZE;
- }
-
- char *myalloc(s)
- unsigned int s;
- {
- register char *r;
- extern char *malloc();
-
- if((r = malloc(s)) == (char *)NULL)
- {
- fprintf(stderr,"Out of memory\n");
- exit(8);
- }
- return r;
- }
-
- addpage(i, num)
- int i, num;
- {
- register PAGELIST *p, *prev;
- register PAGELIST *q;
-
- q = (PAGELIST *)myalloc(sizeof(PAGELIST));
- q->page = num;
- q->next = (PAGELIST *)NULL;
-
- if((p = itemlist[i]->pagelist) == (PAGELIST *)NULL)
- {
- itemlist[i]->pagelist = q;
- return;
- }
-
- for(prev = p; p != (PAGELIST *)NULL; p = p->next)
- {
- if(p->page == num)
- {
- free(q);
- return;
- }
- if(p->page > num)
- {
- q->next = p;
- if(p != itemlist[i]->pagelist)
- prev->next = q;
- else
- itemlist[i]->pagelist = q;
- return;
- }
- prev = p;
- }
-
- prev->next = q;
- }
-
-
-
- printout()
- {
- register int i;
- register PAGELIST *p;
- register char fc;
- register char *s;
-
- fprintf(out,"\\begin{theindex}\n");
-
- for(fc = itemlist[0]->name[0], i = 0; i < nitem; i++)
- {
- if(*(s = itemlist[i]->name) != fc)
- {
- fprintf(out, "\\indexspace\n");
- fc = *s;
- }
-
-
- fprintf(out, "\\item %s", s);
- for (p = itemlist[i]->pagelist; p != (PAGELIST *)NULL; p =
- p->next)
- {
- if(p->next == (PAGELIST *)NULL)
- fprintf(out, " %d", p->page);
- else
- fprintf(out," %d,", p->page);
- }
- fprintf(out, "\n");
- }
- fprintf(out,"\\end{theindex}\n");
-
- }
-
- #ifdef DLIBS
- perror(s)
- char *s;
- {
- fprintf(stderr,"Cannot Open %s\n", s);
- }
- #endif
-